package com.xuegao.xuegaololskills.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;

/**
 * <br/> @PackageName：com.xuegao.xuegaololskills.swing
 * <br/> @ClassName：ajhdadsada
 * <br/> @Description：
 * <br/> @author：xuegao
 * <br/> @date：2020/11/15 15:05
 */
public class ajhdadsada {
    public static void main(String[] args) {
        JFrame frame = new asdadsFrame("Swing Demo");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(500, 400);// 设置窗口的其他参数，如窗口大小

        frame.setVisible(true);// 显示窗口

    }
}

class asdadsFrame extends JFrame {
    adadadaPanel panel = new adadadaPanel(this);

    public asdadsFrame(String title) {
        super(title);
        this.setContentPane(panel); //设置内容面板
        this.setAlwaysOnTop(false);
        panel.setLayout(null);
    }
}

class adadadaPanel extends JScrollPane {
    JTextArea textArea = new JTextArea();  //创建文本区域

    public adadadaPanel(asdadsFrame asdadsFrame) {
        this.setFocusable(true);
        textArea.setBounds(5, 5, 300, 200); //设置文本区域的大小
        // textArea.addKeyListener(new key_hander(asdadsFrame));
        Toolkit tk = Toolkit.getDefaultToolkit();
        tk.addAWTEventListener(new ImplAWTEventListener(), AWTEvent.KEY_EVENT_MASK);
        this.add(textArea);
    }

    //创建响应键盘事件监听器类

    class key_hander implements KeyListener {
        // private Boolean always = Boolean.FALSE;
        private asdadsFrame asdadsFrame;

        public key_hander(com.xuegao.xuegaololskills.swing.asdadsFrame asdadsFrame) {
            this.asdadsFrame = asdadsFrame;
        }

        @Override
        public void keyPressed(KeyEvent e) {
            System.out.println("keyPressed" + KeyEvent.getKeyText(e.getKeyCode()) + "键\n");
            if (e.getID() == KeyEvent.KEY_PRESSED) {
                int code = e.getKeyCode();

                if (code == KeyEvent.VK_F1) {
                    textArea.append("键盘" + KeyEvent.getKeyText(e.getKeyCode()) + "键按下\n");
                }
                if (code == KeyEvent.VK_TAB) {
                    // always = true;
                    asdadsFrame.setAlwaysOnTop(true);
                }
            }
        }

        @Override

        public void keyReleased(KeyEvent e) {
            System.out.println("keyReleased" + KeyEvent.getKeyText(e.getKeyCode()) + "键松开\n");
            asdadsFrame.setAlwaysOnTop(false);
            // System.out.println(" keyReleased " + e.getKeyCode() + "==" + e.getKeyChar());
        }

        // f卡头的按键不会走下面
        @Override
        public void keyTyped(KeyEvent e) {
            //text.append("输入内容:" + e.getKeyChar() + "\n");
            System.out.println("keyTyped" + KeyEvent.getKeyText(e.getKeyCode()) + "键\n");
            if (e.getID() == KeyEvent.KEY_TYPED) {
                char ch = e.getKeyChar();

                textArea.append("输入内容:" + e.getKeyChar() + "\n");

            }

        }

    }

}

class ImplAWTEventListener implements AWTEventListener {
    @Override
    public void eventDispatched(AWTEvent event) {
        if (event.getClass() == KeyEvent.class) {
// 被处理的事件是键盘事件.
            KeyEvent keyEvent = (KeyEvent) event;
            if (keyEvent.getID() == KeyEvent.KEY_PRESSED) {
//按下时你要做的事情
                keyPressed(keyEvent);
            } else if (keyEvent.getID() == KeyEvent.KEY_RELEASED) {
//放开时你要做的事情
                keyReleased(keyEvent);
            }
        }
    }

    private void mousePressed(MouseEvent event) {
    }

    private void keyReleased(KeyEvent event) {
    }

    private void keyPressed(KeyEvent event) {
        System.out.println("eventDispatched");
    }
}